package com.chookss.video

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.WindowManager
import androidx.lifecycle.ViewModelProviders
import com.aliyun.apsara.alivclittlevideo.net.NetWatchdog
import com.chookss.R
import com.chookss.base.StaticClass
import com.chookss.tools.MyEvent
import com.chookss.tools.NavigationTools
import com.chookss.tools.Utils
import com.chookss.video.model.VideoModel
import com.gyf.immersionbar.ImmersionBar
import com.johnrambo.ktea.ktExtends.ShareUtils
import com.lvgroup.hospital.base.BaseAct
import kotlinx.android.synthetic.main.activity_base_video_play.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/27 16:17
 * Note:
 */
abstract class BaseVideoPlayActivity : BaseAct() {
    private val TAG = "BaseVideoPlayActivity"
    private  var watermark: String? =null
    protected val handler = Handler(Looper.getMainLooper())
    protected lateinit var videoPlayView: AlivcVideoPlayView
    protected lateinit var statusBar:View
    protected val videoModel by lazy {
        ViewModelProviders.of(this).get(VideoModel::class.java)
    }

    private var netWatchdog: NetWatchdog? = null
    private var lpFlag:String = "" //0禁止1正常

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!initOnCreate()) {
            finish()
            return
        }
        watermark=intent.getStringExtra("watermark")
        setContentView(R.layout.activity_base_video_play)
        lpFlag = ShareUtils().getString(this, StaticClass.LP_FLAG, "1")
        //禁止截屏
        if ("0".equals(lpFlag)) {
            window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
        }
        ImmersionBar.with(this)
            .transparentStatusBar() /*透明状态栏，不写默认透明色*/ /*状态栏字体是深色，不写默认为亮色*/
            .statusBarDarkFont(false)
            .flymeOSStatusBarFontColor(R.color.white) /*修改flyme OS状态栏字体颜色*/
            .keyboardEnable(false).navigationBarEnable(false)
            .init()
        Utils.keepScreenLongLight(this, true)
        ivLeft.setOnClickListener { onBackPressed() }

        initNetWatchDog()
        initVideoPlayView()
        loadPlayList(false)
        NavigationTools.saveUseRecord(mContext, "1", "19")
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun onMoonEvent(messageEvent: MyEvent) {
        //事件订阅者处理事件
        if (messageEvent.hashMap != null) {
            val map: Map<String, String?> =
                messageEvent.hashMap
        } else {
            val msg = messageEvent.message
        }
    }

    protected open fun initOnCreate() = true

    /**
     * 网络监听
     */
    fun initNetWatchDog() {
        netWatchdog = NetWatchdog(this)
        netWatchdog!!.setNetConnectedListener(MyNetConnectedListener(this))
    }

    /**
     * 初始化View
     */
    protected open fun initVideoPlayView() {
        videoPlayView = findViewById(R.id.video_play)
        videoPlayView.videoListView.setEmptyTv(1)
        statusBar = findViewById(R.id.statusBar)
        //顶部状态栏 处理
        val statusBarHeight = Utils.getStatusBarHeight(this)
        val topAllParams = statusBar.getLayoutParams()
        topAllParams.height = statusBarHeight
        statusBar.setLayoutParams(topAllParams)
        // 可以自定义水印文字颜色、大小和旋转角度
        if(!Utils.isNull(watermark)){
            NavigationTools.setWaterMark(this, watermark)
        }

        videoPlayView!!.setOnRefreshDataListener(object : AlivcVideoListView.OnRefreshDataListener {
            override fun onRefresh() {
                loadPlayList(false)
            }

            override fun onLoadMore() {
                loadPlayList(true)
            }
        })

        videoPlayView.setOnItemBtnClick(createOnVideoButtonClickListener())
    }

    protected open fun createOnVideoButtonClickListener() = OnVideoButtonClickListener(videoModel, this)

    protected abstract fun loadPlayList(isLoadMoreData: Boolean)

    override fun onResume() {
        super.onResume()
        videoPlayView!!.onResume()
    }

    override fun onPause() {
        super.onPause()
        videoPlayView!!.onPause()
    }

    override fun onStart() {
        super.onStart()
        netWatchdog!!.startWatch()
    }

    override fun onStop() {
        super.onStop()
        netWatchdog!!.stopWatch()
    }

    override fun onDestroy() {
        try {
            if (videoPlayView != null) {
                videoPlayView!!.setOnRefreshDataListener(null)
                videoPlayView!!.onDestroy()
            }
        } catch (e: Exception) {
        }
        super.onDestroy()
    }

}